﻿using GameControler.Emgu;
using PlatformCommon.Manager;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace GameControler.Game
{
    public class GoOnView : ViewObject
    {

        public override string FloderName
        {
            get { return "GoOnView"; }
        }

        public GoOnView()
            : base()
        {

        }

        public Point? ClickPoint
        {
            private set;
            get;
        }

        public override bool IsCurrentView(System.Drawing.Bitmap intputbmp)
        {

            foreach (var item in this.Templates)
            {
                var mdhsv = JilyEmgu.MatchAllResultHSV(intputbmp, item);
                if (mdhsv.IsMatch)
                {
                    ClickPoint = mdhsv.Point;
                    Log4Wrap.Logger.Info("GoOn界面匹配" + "__mdhsv_" + mdhsv.TemplateView.Name + "___" + ClickPoint.Value);

                    return true;
                }
                var mdgray = JilyEmgu.MatchAllResultGray(intputbmp, item);
                if (mdgray.IsMatch)
                {
                    ClickPoint = mdgray.Point;
                    Log4Wrap.Logger.Info("GoOn界面匹配" + "__mdgray_" + mdgray.TemplateView.Name + "___" + ClickPoint.Value);
                    return true;
                }
                var mdrgb = JilyEmgu.MatchAllResultRGB(intputbmp, item, 0.2d);
                if (mdrgb.IsMatch)
                {
                    ClickPoint = mdrgb.Point;
                    Log4Wrap.Logger.Info("GoOn界面匹配" + "__mdrgb_" + mdrgb.TemplateView.Name + "___" + ClickPoint.Value);
                    return true;
                }
            }

            var teamtemplate = new List<TemplateView>();
            var mds = new List<MatchData>();
            MatchData first = null;

            foreach (var item in this.Templates)
            {
                var md = JilyEmgu.MatchResult(intputbmp, item);
                if (md.IsMatch)
                {
                    if (first == null)
                    {
                        first = md;
                        mds.Add(md);
                    }
                    else if (first.TemplateView.Team == item.Team)
                    {
                        mds.Add(md);
                    }
                    //else if (item.Team == "2")
                    //{
                    //    first = md;
                    //    mds.Clear();
                    //    mds.Add(md);
                    //}
                }
            }
            if (mds.Count != 0)
            {
                foreach (var item in mds)
                {
                    Log4Wrap.Logger.Info("GoOn界面匹配" + "___" + item.TemplateView.Name + "___" + item.Point);
                    GameShow.GameHandle.SendClick(item.Point);
                }

                //var realdata = mds.OrderBy(i => i.MatchRate).FirstOrDefault();
                //ClickPoint = realdata.Point;

                //Log4Wrap.Logger.Info("GoOn界面匹配" + "___" + realdata.TemplateView.Name + "___" + ClickPoint.Value);

                return true;
            }

            return false;
        }


        //public override bool IsCurrentView(System.Drawing.Bitmap intputbmp)
        //{
        //    var teamtemplate = new List<TemplateView>();
        //    var mds = new List<MatchData>();
        //    foreach (var item in this.Templates)
        //    {
        //        var md = JilyEmgu.MatchResult(intputbmp, item);
        //        if (md.IsMatch)
        //        {
        //            teamtemplate = new List<TemplateView>(this.Templates.Where(i => !string.IsNullOrWhiteSpace(item.Team) && i.Team == item.Team && i.Name != item.Name));
        //            mds.Add(md);
        //            break;
        //        }
        //    }

        //    if (mds.Count != 0)
        //    {
        //        if (teamtemplate.Count != 0)
        //        {
        //            foreach (var item in teamtemplate)
        //            {
        //                var md = JilyEmgu.MatchResult(intputbmp, item);
        //                if (md.IsMatch)
        //                {
        //                    mds.Add(md);
        //                }
        //            }
        //        }

        //        var realdata = mds.OrderBy(i => i.MatchRate).LastOrDefault();
        //        ClickPoint = realdata.Point;
        //        return true;
        //    }

        //    return false;
        //}

    }
}
